namespace PHSRAG.Insight21.FormsManagement.Controls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Text;

	/// <summary>
	///	DropDownMultipleSelection is the UI displayed to the user when a DropDownMultiSelectQuestion is rendered
	/// </summary>
    public partial class Migrated_DropDownMultipleSelection : DropDownMultipleSelection
	{
		private string parentName;
		private string parentValue;
		private string items;
		private string dropDownListItemTextStrings;
		private string dropDownListItemValues;
		private string selectedListItemText;

		/// <summary>
		/// Gets or sets the parent question name for the DropDowmMultiSelect question
		/// </summary>
		override public string ParentName
		{
			get { return parentName; }
			set { parentName = value; }
		}


		/// <summary>
		/// Gets or sets the parent value for the DropDowmMultiSelect question
		/// </summary>
		override public string ParentValue
		{
			get { return parentValue; }
			set { parentValue = value; }
		}


		/// <summary>
		/// Gets or sets the '^' delimited list of items for the listbox in DropDowmMultiSelect control
		/// </summary>
		override public string Items
		{
			get 
			{
				int itemCount = itemsList.Items.Count;
				StringBuilder items = new StringBuilder();
				for (int i=0; i<itemCount; i++)
				{
					items.Append(string.Format("{0}^", itemsList.Items[i].Text));
				}
				string allItems = items.ToString();
				if (allItems.Length > 0)
					allItems = allItems.Substring(1, allItems.Length - 1);
				return allItems;
			}
			set
			{
				items = value;
				
			}
		}


		/// <summary>
		/// Sets the text values(concatenated by a '^') for the items to be displayed in the dropdownlist box. These items are predefined
		/// at the question creation time.
		/// </summary>
		override public string DropDownListItemTextStrings
		{
			set
			{
				dropDownListItemTextStrings = value;
			}
		}


		/// <summary>
		/// Sets the values(concatenated by a '^') for the items to be displayed in the dropdownlist box. Each value is a unique identifier
		/// for the corresponding text displayed in the dropdownlist box.
		/// </summary>
		override public string DropDownListItemValues
		{
			set
			{
				dropDownListItemValues = value;
			}
		}

		
		/// <summary>
		/// Gets or sets the selected item in the list box
		/// </summary>
		override public string SelectedListItemText
		{
			get
			{
				return (itemsList.SelectedIndex >=0) ? itemsList.SelectedItem.Value : string.Empty;
			}
			set
			{
				selectedListItemText = value;
			}
		}


		/// <summary>
		/// Loads options into the dropdownlist box and the list box. Each option has an associated text that is visible to the user and value that uniquely 
		/// identifies the option.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnPageLoad(object sender, System.EventArgs e)
		{
			string[] textStrings = dropDownListItemTextStrings.Split('^');
			string[] values = dropDownListItemValues.Split('^');
			int listItemCount = textStrings.Length;
			newItem.Items.Add(new ListItem("-- Select --", "Select"));
			for (int i=0; i<listItemCount; ++i)
			{
				newItem.Items.Add(new ListItem(textStrings[i], values[i]));
			}

			itemsList.Items.Clear();
			string[] itemArray = items.Split('^');
			int itemCount = itemArray.Length; 
			for (int i=0; i<itemCount; ++i)
			{
				if (itemArray[i] != string.Empty)
				{
					if (newItem.Items.FindByValue(itemArray[i]) != null)
					{
						itemsList.Items.Add(new ListItem(newItem.Items.FindByValue(itemArray[i]).Text, itemArray[i]));
						if (itemArray[i] == selectedListItemText)
						{
							itemsList.SelectedIndex = itemsList.Items.Count - 1;
						}
					}
				}
			}
				
			if (itemsList.Items.Count>0)
			{
				removeItem.Visible = true;
			}
		}

		
		#region Web Form Designer generated code
		/// <summary>
		/// Page Initialization
		/// </summary>
		/// <param name="e">Arguments associated with the event.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			base.OnInit(e);
			InitializeComponent();
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnPageLoad);
		}
		#endregion

		/// <summary>
		/// If not already added, adds the selected option to the list box. This option is also added to the answers list of the corresponding question.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnAddItemClick(object sender, EventArgs e)
		{
			string newItemText = newItem.SelectedItem.Text;
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			if(newItemText.IndexOf("--") == -1 && itemsList.Items.FindByText(newItemText) == null)
			{
				itemsList.Items.Add(new ListItem(newItemText, newItem.SelectedItem.Value));
				removeItem.Visible = true;
				viewer.AddMultiSelectItem(parentName, parentValue, ID.Substring(ID.IndexOf("-") + 1), newItem.SelectedItem.Value);
			}
			else
			{
				viewer.Refresh();
			}

		}


		/// <summary>
		/// Removes the selected option from the list box. This option is also deleted from the answers list of the corresponding question.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnRemoveItemClick(object sender, EventArgs e)
		{
			int selectedIndex = itemsList.SelectedIndex;
			string removedItem = itemsList.SelectedItem.Value;
			if (selectedIndex >= 0)
				itemsList.Items.RemoveAt(selectedIndex);
			if (itemsList.Items.Count == 0)
			{
				removeItem.Visible = false;
			}
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			viewer.RemoveMultiSelectItem(parentName, parentValue, ID.Substring(ID.IndexOf("-") + 1), removedItem);
		}


		/// <summary>
		/// Based on the option selected, the answers for the child questions, if any, are displayed.
		/// </summary>
		/// <param name="sender">Control that raised the event.</param>
		/// <param name="e">Arguments associated with the event.</param>
		protected void OnItemsListSelectedIndexChanged(object sender, System.EventArgs e)
		{
			QuestionnaireViewer viewer = (this as System.Web.UI.UserControl).NamingContainer as QuestionnaireViewer;
			viewer.Refresh();
		}

	}
}
